package com.maxlen.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.Query;
import com.maxlen.goods.dao.BrandDao;
import com.maxlen.goods.dao.CategoryBrandRelationDao;
import com.maxlen.goods.dao.CategoryDao;
import com.maxlen.goods.entity.BrandEntity;
import com.maxlen.goods.entity.CategoryBrandRelationEntity;
import com.maxlen.goods.entity.CategoryEntity;
import com.maxlen.goods.entity.dto.CategoryBrandRelationWithBrandDTO;
import com.maxlen.goods.entity.dto.SkuItemDTO;
import com.maxlen.goods.entity.vo.CategoryBrandRelationVO;
import com.maxlen.goods.entity.dto.CategoryBrandRelationDTO;
import com.maxlen.goods.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> findByBrandId(Long brandId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryBrandRelationEntity::getBrandId, brandId);
        List<CategoryBrandRelationEntity> relationEntityList = baseMapper.selectList(wrapper);

        return relationEntityList;
    }

    @Override
    public void saveDetail(CategoryBrandRelationVO categoryBrandRelationVO) {

        Long brandId = categoryBrandRelationVO.getBrandId();
        Long catelogId = categoryBrandRelationVO.getCatelogId();
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();

        Long aLong = baseMapper.selectCount(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catelogId)
                .eq(CategoryBrandRelationEntity::getBrandId, brandId));
        if(aLong.equals(0L)){
            BrandEntity brand = brandDao.selectById(brandId);
            CategoryEntity category = categoryDao.selectById(catelogId);
            relationEntity.setBrandId(brandId);
            relationEntity.setCatelogId(catelogId);
            relationEntity.setBrandName(brand.getName());
            relationEntity.setCatelogName(category.getName());
        }

        baseMapper.insert(relationEntity);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandName(name);
        baseMapper.update(entity, new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getBrandId, brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogName(name);
        baseMapper.update(entity, new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId, catId));
    }

    @Override
    public void removeBatchByBrandId(List<Long> brandIds) {
        baseMapper.removeBatchByBrandId(brandIds);
    }

    @Override
    public List<CategoryBrandRelationEntity> findByCategoryId(Long categoryId) {
        if(categoryId == null){
            return new ArrayList<>();
        }
        List<CategoryBrandRelationEntity> entityList = baseMapper.selectList(new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId, categoryId));
        return entityList;
    }

    @Override
    public List<SkuItemDTO.BrandDetailDTO> getBrandByCatalogId(Long catalogId) {

        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryBrandRelationEntity::getCatelogId, catalogId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = baseMapper.selectList(wrapper);

        List<SkuItemDTO.BrandDetailDTO> brandDetailDTOList = new ArrayList<>();
        SkuItemDTO.BrandDetailDTO brandDetailDTO;
        if(!categoryBrandRelationEntities.isEmpty()){
            for (CategoryBrandRelationEntity categoryBrandRelationEntity : categoryBrandRelationEntities) {
                brandDetailDTO = new SkuItemDTO.BrandDetailDTO();
                brandDetailDTO.setBrandId(categoryBrandRelationEntity.getBrandId());
                brandDetailDTO.setName(categoryBrandRelationEntity.getBrandName());
                brandDetailDTOList.add(brandDetailDTO);
            }
        }
        return brandDetailDTOList;

    }

}
